package com.yy.wga.common.core.dto;

import com.github.pagehelper.Page;
import lombok.Data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页公共类
 * @author lgw
 */
@Data
public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 8656597559014685635L;
    /**
     * 总记录数
     */
    private long total;
    /**
     * 结果集
     */
    private List<T> list;
    /**
     * 第几页
     */
    private int pageNum;
    /**
     * 每页记录数
     */
    private int pageSize;
    /**
     * 总页数
     */
    private int pages;
    /**
     * 当前页的数量 <= pageSize，该属性来自ArrayList的size属性
     */
    private int size;

    /**
     * @param list
     */
    public PageBean(List<T> list) {
        if (list instanceof Page) {
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }
    }

    /**
     * 设置分页信息
     *
     * @param list
     */
    public void setPageInfo(List<?> list) {
        if (list instanceof Page) {
            Page<?> page = (Page<?>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.size = page.size();
            this.list = Collections.emptyList();
        }
    }

    public PageBean() {
        this.total = 0;
        this.pages = 0;
        this.size = 0;
        this.list = Collections.emptyList();
    }

}